﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThinhTien.Common.Enums;
using DevExpress.Web.Mvc;
using ThinhTien.Data.Repository;

namespace ThinhTien.Admin.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Order/

        public ActionResult Index()
        {
            Session["MenuItem"] = MenuItems.Order;
            return View();
        }

        #region Paging
        public ActionResult OrderRowPartial()
        {
            var viewModel = GridViewExtension.GetViewModel("gvOrder");
            if (viewModel == null)
            {
                viewModel = CreateGridViewModel();
            }
            return CustomBindingCore(viewModel);
        }

        public ActionResult ApplyFilterExpressionPartial(string filterExpression)
        {
            var viewModel = GridViewExtension.GetViewModel("gvOrder");
            viewModel.FilterExpression = filterExpression;
            return CustomBindingCore(viewModel);
        }

        public ActionResult OrderSortingRowPartial(GridViewColumnState column, bool reset)
        {
            var viewModel = GridViewExtension.GetViewModel("gvOrder");

            viewModel.SortBy(column, reset);
            return CustomBindingCore(viewModel);
        }

        private GridViewModel CreateGridViewModel()
        {
            var viewModel = new GridViewModel();
            viewModel.KeyFieldName = "ID";
            viewModel.Columns.Add("Name");
            viewModel.Columns.Add("Description");
            viewModel.Columns.Add("Position");
            viewModel.Columns.Add("IsHomePage");
            return viewModel;
        }

        public ActionResult OrderPagingRowPartial(GridViewPagerState pager)
        {
            var viewModel = GridViewExtension.GetViewModel("gvOrder");
            viewModel.Pager.Assign(pager);
            return CustomBindingCore(viewModel);
        }

        public ActionResult OrderFilteringRowPartial(GridViewColumnState column)
        {
            var viewModel = GridViewExtension.GetViewModel("gvOrder");
            viewModel.Columns[column.FieldName].Assign(column);
            return CustomBindingCore(viewModel);
        }

        PartialViewResult CustomBindingCore(GridViewModel gridViewModel)
        {
            gridViewModel.ProcessCustomBinding(
                OrderBindingHandler.GetDataRowCount,
                OrderBindingHandler.GetData
            );

            return PartialView("OrderGridPartial", gridViewModel);
        }
        #endregion

    }
}
